package com.ming.stringstudy.feedbacksystem.controller;

import com.ming.stringstudy.feedbacksystem.entity.Comment;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.pojo.Result;
import com.ming.stringstudy.feedbacksystem.service.ICommentService;
import com.ming.stringstudy.feedbacksystem.utils.HeaderName;
import com.ming.stringstudy.feedbacksystem.utils.JwtUtils;
import com.ming.stringstudy.feedbacksystem.utils.PermissionUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

import static com.ming.stringstudy.feedbacksystem.utils.PermissionConstants.*;

/**
 * <p>
 * 意见反馈 前端控制器
 * </p>
 *
 * @author aming
 * @since 2024-12-24
 */
@Slf4j
@RestController
@RequestMapping("/comments")
public class CommentController<T> {
    public final ICommentService commentService;
    public final PermissionUtils permissionUtils;

    public CommentController(ICommentService commentService, PermissionUtils permissionUtils) {
        this.commentService = commentService;
        this.permissionUtils = permissionUtils;
    }

    // 新增意见/问题
    @PostMapping
    public Result insertComment(@RequestBody Comment comment ,
                                @RequestAttribute("userId") Integer userId) {
        // 解析令牌注入UserId
        comment.setUserId(userId);
        log.info(comment.toString());
        //新增意见
        if (commentService.insertComment(comment) == -1){
            return Result.error("没有这个类型");
        }
        return Result.success();
    }
    //根据不同条件对问题进行分页查询
    @GetMapping
    public Result conditionPageList(Comment comment,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestAttribute("userId") String userId,
                                 @RequestAttribute("deptId") String deptId
    ) {
        if(!userId.isEmpty())comment.setUserId(Integer.parseInt(userId));
        if(!deptId.isEmpty())comment.setDeptId(Integer.parseInt(deptId));
        log.info("comment{}, begin: {}, end: {}, page: {}, pageSize: {}",
                comment, begin, end, page, pageSize);
        PageBean pageBean = commentService.conditionPageList(comment,begin,end,page,pageSize);
        return Result.success(pageBean);
    }

    //老师角色意见回退
    @PostMapping("/return")
    public Result commentReturn(@RequestParam Integer id, @RequestParam(defaultValue = "null") String return_reason, HttpServletRequest request) {
        try {
            String jwt = request.getHeader(HeaderName.headerName);
            Claims claims = JwtUtils.parserJwt(jwt);
            int roleId = claims.get("roleId", Integer.class);
            int deptId = claims.get("deptId", Integer.class);

            if (permissionUtils.equalPermission(roleId, ISSUE_REVERT)) {
                int commentTypeId = commentService.getCommentTypeById(id);
                if (deptId == commentTypeId) { // 如果有权限并且是对应部门
                    commentService.commentReturn(id, return_reason);
                    return Result.success("意见回退成功");
                } else {
                    return Result.error("非同一部门意见");
                }
            } else {
                return Result.error("没有权限");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("意见回退失败");
        }
    }
    //管理员角色意见取回
    @PostMapping("/retrieve")
    public Result commentRetrieve(@RequestParam Integer id,
                                  HttpServletRequest request) {
        try{
            String jwt = request.getHeader(HeaderName.headerName);
            Claims claims = JwtUtils.parserJwt(jwt);
            int roleId = claims.get("roleId", Integer.class);
            if (permissionUtils.equalPermission(roleId, ISSUE_RETRIEVE)) {
                // 如果有权限
                commentService.commentRetrieve(id);
                return Result.success("意见取回成功");
            } else {
                return Result.error("没有权限");
            }

        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("意见取回失败");
        }
    }
    //管理员角色意见重新分派
    @PostMapping("/reassign")
    public Result commentReassign(@RequestParam Integer id ,Integer comment_type_id,
                                  HttpServletRequest request) {
        try{
            String jwt = request.getHeader(HeaderName.headerName);
            Claims claims = JwtUtils.parserJwt(jwt);
            int roleId = claims.get("roleId", Integer.class);
            if (permissionUtils.equalPermission(roleId, REASSIGN)) {
                // 有权限
                commentService.commentReassign(id, comment_type_id);
                return Result.success("意见重新分派成功");
            } else {
                return Result.error("没有权限");
            }

        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("意见重新分派失败");
        }
    }
    //管理员意见归档
    @PostMapping("/archive")
    public Result commentArchive(@RequestParam Integer id,HttpServletRequest request) {
        try{
            String jwt = request.getHeader(HeaderName.headerName);
            Claims claims = JwtUtils.parserJwt(jwt);
            int roleId = claims.get("roleId", Integer.class);
            if (permissionUtils.equalPermission(roleId, DEPARTMENT_ARCHIVE)) {
                // 如果有权限
                commentService.commentArchive(id);
                return Result.success("意见归档成功");
            } else {
                return Result.error("没有权限");
            }

        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("意见归档失败");
        }
    }
    //返回老师角色回退意见
    @GetMapping("/selectreturn")
    public Result selectcommentReturn(HttpServletRequest request) {
        try{
            String jwt = request.getHeader(HeaderName.headerName);
            Claims claims = JwtUtils.parserJwt(jwt);
            int roleId = claims.get("roleId", Integer.class);
            if (permissionUtils.equalPermission(roleId, ISSUE_REVERT)) {
                List<Comment> comments = commentService.selectCommentReturn();
                return Result.success(comments);
            }else {
                return Result.error("没有权限");
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("服务器内部错误");
        }
    }
    //返回管理员角色取回意见
    @GetMapping("/selectretrieve")
    public Result selectommentRetrieve(HttpServletRequest request) {
        try{
            String jwt = request.getHeader(HeaderName.headerName);
            Claims claims = JwtUtils.parserJwt(jwt);
            int roleId = claims.get("roleId", Integer.class);
            if (permissionUtils.equalPermission(roleId, ISSUE_RETRIEVE)) {
                List<Comment> comments=commentService.selectCommentRetrieve();
                return Result.success(comments);
            }else {
                return Result.error("没有权限");
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("服务器内部错误");
        }
    }

    //返回管理员角色未回复意见
    @GetMapping("/selectreassign")
    public Result selectcommentReassign(HttpServletRequest request) {
        try{

            String jwt = request.getHeader(HeaderName.headerName);
            Claims claims = JwtUtils.parserJwt(jwt);
            int roleId = claims.get("roleId", Integer.class);
            if (permissionUtils.equalPermission(roleId, REASSIGN)) {
                List<Comment> comments = commentService.selectCommentReassign();
                return Result.success(comments);
            }else {
                return Result.error("没有权限");
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("服务器内部错误");
        }
    }


    //返回管理员归档意见
    @GetMapping("/selectarchive")
    public Result selectcommentArchive(HttpServletRequest request) {
        try{
            String jwt = request.getHeader(HeaderName.headerName);
            Claims claims = JwtUtils.parserJwt(jwt);
            int roleId = claims.get("roleId", Integer.class);
            if (permissionUtils.equalPermission(roleId, DEPARTMENT_ARCHIVE)) {
                List<Comment> comments = commentService.selectCommentArchive();
                return Result.success(comments);
            }else {
                return Result.error("没有权限");
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("服务器内部错误");
        }
    }
}
